package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.FlexiblePackageLogic;
import com.hotel.BL.GuestTypeLogic;
import com.hotel.BL.RoomTypeLogic;

import data.FlexiblePackage;
import data.GuestType;
import data.RoomType;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class FlexiblePackageService extends HttpServlet{
	//http://localhost:8080/flexpackageservice?func=addfixedpackage
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("getflexiblepackagedata")){
				GetAddFlexiblePackagePageData(req,resp);
			}else if(doClass.equals("addflexiblepackage")){
				AddFlexiblePackage(req,resp);
			}else if(doClass.equals("editflexiblepackage")){
				EditFlexiblePackage(req,resp);
			}else if(doClass.equals("getAllFlexible4Admin")){
				GetAllFlexible4AdminLimit(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("del")){
				DeleteFlexiblePackage(req,resp);
			}else if(doClass.equals("archived")){
				ChangeArchived(req, resp);
			}else if(doClass.equals("restore")){
				RestoreArchived(req, resp);
			}else if(doClass.equals("getAllFlexibleArchive4Admin")){
				GetAllFlexibleArchive4AdminLimit(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void GetAddFlexiblePackagePageData(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");

		HashMap<String, Object> hash = new HashMap<String,Object>();
		List<GuestType> guesttype = new GuestTypeLogic().GetAllGuestType(hotelId);
		List<RoomType> roomtype = new RoomTypeLogic(req,resp).GetAllRoomType(hotelId);

		if(req.getParameter("id") != null){
			long id = Long.parseLong(req.getParameter("id"));
			FlexiblePackage flex = new FlexiblePackageLogic().GetFlexiblePackage(id);
			hash.put("data", new FlexiblePackageLogic().ChangeFormatForEditFlexiblePackage(flex));
		}		
		hash.put("guesttype",new GuestTypeLogic().OnlyIdAndTitle(guesttype));
		hash.put("roomtype", roomtype);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hash));
	}
	
	private void AddFlexiblePackage(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException, ParseException
	{	
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String dateArray = req.getParameter("dateArray");
		String packageName = req.getParameter("packagename");
		String roomtypeString = req.getParameter("roomtype");
		String availableForString = req.getParameter("guesttype");
		Date datefrom = formatter.parse(req.getParameter("datefrom"));
		Date dateto = formatter.parse(req.getParameter("dateto"));
		boolean status = Boolean.parseBoolean(req.getParameter("status"));
		String description = req.getParameter("description");
		String ppnArray = req.getParameter("ppnArray");
		String ucpArray = req.getParameter("ucppArray");
		String extrabedArray = req.getParameter("exbedArray");
		String adultArray = req.getParameter("adultArray");
		String childArray = req.getParameter("childArray");
		String breakfastArray = req.getParameter("breakfastArray");
		String subflexiblepackagestring = new FlexiblePackageLogic().
		GetSubFlexiblePackageString(datefrom, dateto,ppnArray,ucpArray,extrabedArray,
				adultArray,childArray,breakfastArray,dateArray);
		new FlexiblePackageLogic().AddFlexiblePackage(hotelId, availableForString,
				status, datefrom, dateto, description, packageName, roomtypeString,
				subflexiblepackagestring);
			GetAllFlexible4AdminLimit(req, resp);
	}

	private void EditFlexiblePackage(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException, ParseException
	{	
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		long id = Long.parseLong(req.getParameter("id"));
		String packageName = req.getParameter("packagename");
		String roomtypeString = req.getParameter("roomtype");
		String availableForString = req.getParameter("guesttype");
		Date datefrom = formatter.parse(req.getParameter("datefrom"));
		Date dateto = formatter.parse(req.getParameter("dateto"));
		boolean status = Boolean.parseBoolean(req.getParameter("status"));
		String description = req.getParameter("description");
		String dateArray = req.getParameter("dateArray");
		String ppnArray = req.getParameter("ppnArray");
		String ucpArray = req.getParameter("ucppArray");
		String extrabedArray = req.getParameter("exbedArray");
		String adultArray = req.getParameter("adultArray");
		String childArray = req.getParameter("childArray");
		String breakfastArray = req.getParameter("breakfastArray");
		String pIdArray = req.getParameter("pId");
		String subflexiblepackagestring = new FlexiblePackageLogic().
		EditSubFlexiblePackageString(pIdArray,datefrom, dateto,ppnArray,ucpArray,extrabedArray,
				adultArray,childArray,breakfastArray,dateArray);
		new FlexiblePackageLogic().EditFlexiblePackage(id,hotelId, availableForString,
				status, datefrom, dateto, description, packageName, roomtypeString,
				subflexiblepackagestring);
			GetAllFlexible4AdminLimit(req, resp);
	}
	
	private void GetAllFlexible4AdminLimit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<FlexiblePackage> obj = new FlexiblePackageLogic().GetAllFlexiblePackage4AdminLimit(hotelId,offset,line);
		double dataSize = new FlexiblePackageLogic().GetAllFlexiblePackage4Admin(hotelId).size();
		List<HashMap<String, Object>> v = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<obj.size();i++){
			HashMap<String, Object> x = new HashMap<String, Object>();
			x.put("id", obj.get(i).getId());
			x.put("status", obj.get(i).isStatus());
			x.put("name", obj.get(i).getPackageName());
			x.put("roomtype", obj.get(i).getRoomtype().getShortName());
			x.put("to", obj.get(i).getDateto());
			x.put("from", obj.get(i).getDatefrom());
			v.add(x);
		}
		
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", v);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long id = Long.parseLong(req.getParameter("id"));

		FlexiblePackage obj = new FlexiblePackageLogic().ChangeStatus(id);

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void DeleteFlexiblePackage(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String id = req.getParameter("id");
		for(int i = 0; i < id.split(",").length ; i++){
			new FlexiblePackageLogic().DeleteFlexiblePackage(Long.parseLong(id.split(",")[i]));
		}

		GetAllFlexible4AdminLimit(req, resp);	
	}
	
	private void ChangeArchived(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String idList = req.getParameter("id");
		for(int i = 0; i < idList.split(",").length ; i++){
			new FlexiblePackageLogic().ChangeArchivedLogic(Long.parseLong(idList.split(",")[i]));
		}

		GetAllFlexible4AdminLimit(req, resp);	
	}
	
	private void RestoreArchived(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String idList = req.getParameter("id");
		for(int i = 0; i < idList.split(",").length ; i++){
			new FlexiblePackageLogic().ChangeArchivedLogic(Long.parseLong(idList.split(",")[i]));
		}

		GetAllFlexibleArchive4AdminLimit(req, resp);	
	}
	
	private void GetAllFlexibleArchive4AdminLimit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<FlexiblePackage> obj = new FlexiblePackageLogic().GetAllFlexiblePackageArchive4AdminLimit(hotelId,offset,line);
		double dataSize = new FlexiblePackageLogic().GetAllFlexiblePackageArchive4Admin(hotelId).size();
		List<HashMap<String, Object>> v = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i<obj.size();i++){
			HashMap<String, Object> x = new HashMap<String, Object>();
			x.put("id", obj.get(i).getId());
			x.put("status", obj.get(i).isStatus());
			x.put("name", obj.get(i).getPackageName());
			x.put("roomtype", obj.get(i).getRoomtype().getShortName());
			x.put("to", obj.get(i).getDateto());
			x.put("from", obj.get(i).getDatefrom());
			v.add(x);
		}
		
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", v);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
}
